package test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;

import repast.simphony.parameter.Parameters;

import defectsim.SimulationEnvironment;
import defectsim.agents.DefectAgent;
import defectsim.agents.DefectFactory;
import defectsim.agents.Dislocation;
import defectsim.agents.EdgeDislocation;
import defectsim.agents.DefectFactory.DefectType;
import defectsim.material.SlipSystem;

public class DislocationSourceTester extends DefectAgent{

	private Dislocation tester = null;

	private SimulationEnvironment env = SimulationEnvironment.getInstance();
	private double gridScale;
	
	@Override
	public void percept() {
	}

	@Override
	public void mentalUpdate() {
	}

	@Override
	public void act() {
		if (env.getCurrentTick() == 1.0)
			try {
				this.multiply(1000);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}

	public DislocationSourceTester(){
		Parameters p = env.getParameters();
		int gridSize = (Integer)p.getValue("gridsize");		
		double boxLength = (Double)p.getValue("spacelength");
		this.gridScale = gridSize / boxLength;
	}

	private void multiply(int noOfDislocations) throws IOException{
		double[] xx = new double[noOfDislocations];
		double[] yy = new double[noOfDislocations];
		double[] bg = new double[noOfDislocations];
		String osName = System.getProperty("os.name");
		String prefix = "";
		if (osName.contains("Windows"))
			prefix = "D:\\My Java\\RepastSimphony-1.2.0\\workspace\\DefectSim\\misc\\data\\";
		else
			prefix = "/home/altug/graphs/";
		
		/*
		 * Interaction force test data file contains 1000 randomly distributed dislocations. The last dislocation is the tester.
		 */
		String fname = prefix + "InteractionForceTestData.txt";
		try {
			BufferedReader dislocationTestData = new BufferedReader(new FileReader(fname));
			String sCurrentLine;
			try {
				sCurrentLine = dislocationTestData.readLine();
				String[] sXX = sCurrentLine.split(",");
				for (int i = 0; i < sXX.length; i++)
					xx[i] = Double.parseDouble(sXX[i]);
				sCurrentLine = dislocationTestData.readLine();
				String[] sYY = sCurrentLine.split(",");
				for (int i = 0; i < sYY.length; i++)
					yy[i] = Double.parseDouble(sYY[i]);
				sCurrentLine = dislocationTestData.readLine();
				String[] sBG = sCurrentLine.split(",");
				for (int i = 0; i < sBG.length; i++)
					bg[i] = Double.parseDouble(sBG[i]);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String positive = "\u02d4";
		String negative = "\u02d5";
		String alignment = "";
		String outputDirectory = "/home/altug/graphs/";
		BufferedWriter positionData = null;
		fname = outputDirectory + "InteractionForceUnitTestResult.dat";
		positionData = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fname),"UTF8"));
		SlipSystem[] slipsys = env.getMaterial().getSlipSystems();
		for (int i = 0; i < noOfDislocations; i++) {
			EdgeDislocation dislocation = (EdgeDislocation) DefectFactory.createDefect(DefectType.EdgeDislocation);
			dislocation.setSlipSystem(slipsys[0]);
			dislocation.setUnitBurgersVector(bg[i]);
			dislocation.setX(xx[i]);
			dislocation.setY(yy[i]);
			dislocation.setLocationGridX((int)(xx[i] * gridScale));
			dislocation.setLocationGridY((int)(yy[i] * gridScale));
			env.nucleate(dislocation);
			if (i == noOfDislocations - 1)
				tester = dislocation;
			
			if (dislocation.getUnitBurgersVector() > 0)
				alignment = positive;
			else
				alignment = negative;
			String line = 	String.format("%f", dislocation.getX()) + ";" + 
			String.format("%f", dislocation.getY()) + ";" +
			String.format("%f", dislocation.getEffectiveStress().x) + ";" +
			alignment + ";" +
			String.format("%f", dislocation.getDirection()) + "\n";
			positionData.write(line);
		}
		positionData.close();
	}
	
	public Dislocation getDislocationTester(){
		return tester;
	}
	
}
